<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Links_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
	function add($data = array()){
		$this->mycache->delete('get_case_list_index');
		$this->mycache->delete('get_links_list_index');
		$this->db->insert('zjy_links', $data);
		return $this->db->insert_id();
	}
	

	function update($id, $data = array()){
		$this->mycache->delete('get_case_list_index');
		$this->mycache->delete('get_links_list_index');
		$this->db->where('id', $id);
		$this->db->update('zjy_links', $data);
		return $this->db->affected_rows();
	}
	
	function get_links_obj($id){
		$select = "id,url,webname,logoimg,forder,type";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('zjy_links');
		return $query->row_array();
	}
	
	function get_links_list($data){
		//封装查询内容
		$select = "id, url, webname, logoimg, forder,type";
		
		//封装条件
		$_where = " where is_del=0 ";
		
		if(isset($data['where']['type']) && $data['where']['type'] >0){
			$_where .= " and type=".$data['where']['type'];
		}
		
		if(isset($data['where']['webname']) && $data['where']['webname'] != ''){
			$_where .= " and webname like '%".$data['where']['webname']."%'";
		}
		
		//封装sql
		$sql_result = "select $select from zjy_links";
		$sql_result .= $_where;
		$query = $this->db->query($sql_result);
		$data['links_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from zjy_links";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['links_count'] = $query->row()->count;
		return $data;
	}
	
	function get_links_list_index(){
		$mkey = "get_links_list_index";
		//$this->mycache->delete($mkey);
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		//封装查询内容
		$select = "id, url, webname, logoimg, forder,type";
		$_where = " where is_del=0 and type=1 ";
		
		//封装条件
		
		$_order = " order by forder asc,id asc";
		//封装sql
		$sql_result = "select $select from zjy_links";
	
		$sql_result .= $_where.$_order;
		$query = $this->db->query($sql_result);
		$links_list = $query->result_array();
		$this->mycache->set_array($mkey,$links_list);
		return $links_list;
	}
	
	function get_case_list_index(){
		$mkey = "get_case_list_index";
		//$this->mycache->delete($mkey);
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		//封装查询内容
		$select = "id, url, webname, logoimg, forder,type";
		$_where = " where is_del=0 and type=2 ";
		
		//封装条件
		
		$_order = " order by forder asc,id asc";
		//封装sql
		$sql_result = "select $select from zjy_links";
	
		$sql_result .= $_where.$_order;
		$query = $this->db->query($sql_result);
		$links_list = $query->result_array();
		$this->mycache->set_array($mkey,$links_list);
		return $links_list;
	}
	
}